<?php
declare(strict_types=1);
 

namespace App\Game\Mapper;

use App\Game\Model\GameAccountRtp;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;
use Mine\Helper\Str;

/**
 * 用户rtp设置Mapper类
 */
class GameAccountRtpMapper extends AbstractMapper
{
    /**
     * @var GameAccountRtp
     */
    public $model;

    public function assignModel()
    {
        $this->model = GameAccountRtp::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 游戏平台
        if (isset($params['code']) && filled($params['code'])) {
            $query->where('code', '=', $params['code']);
        }

        return $query;
    }

    public function update(mixed $id, array $data): bool
    {
        $cs = '\\App\\Game\\Api\\Service\\Platform\\' . Str::studly($data['code']) . 'Service';
        if ($cs::setUserRtp([['userId' => '' . $id, 'rtp' => $data['rtp']]])) {
            $s = parent::update($id, $data);
            return $s;
        }
        return false;
    }
}